﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using OrderMgr.SysSetting;
using System.Data.SqlClient;
using TierApplication;


namespace OrderMgr.OrderInfo
{
    public partial class frmBuyPaymentDetailPayFee : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _PayFeeID = "";
        public string _OrderID = "";
        public Utility.OrderType _OrderType;
        public Utility.OrderStatus _AuditStatus;

        T_OrderPayFee mOrderPayFee = new T_OrderPayFee();

        #endregion

        public frmBuyPaymentDetailPayFee()
        {
            InitializeComponent();
        }

        private void frmBuyDetail_Load(object sender, EventArgs e)
        {
            initForm();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Select PayFee,(select isnull(Sum(PayFee),0) from T_OrderPayFee where OrderID=a.OrderID) OrderPayFee");
            strSql.Append(" FROM T_Order a ");
            strSql.Append(" where a.OrderID='" + _OrderID + "'");
            DataSet dsPayedFee = DBUtility.DbHelperSQL.Query(strSql.ToString());
            txtNeedPayFee.Text = dsPayedFee.Tables[0].Rows[0]["PayFee"].ToString();
            txtPayedFee.Text = dsPayedFee.Tables[0].Rows[0]["OrderPayFee"].ToString();

            switch (OpMode)
            {
                case Utility.OperateMode.Query:
                    btnSave.Visible = false;

                    showInfo(_PayFeeID);
                    break;
                case Utility.OperateMode.Insert:
                    txtPayFeeDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    txtPayFeeUserID.EditValue = TierService.Utility.gUserID;
                    double dNeedPayFee = double.Parse(txtNeedPayFee.Text);
                    double dPayedFee = double.Parse(txtPayedFee.Text);
                    txtPayFee.Text =(dNeedPayFee - dPayedFee).ToString();
                    break;
                case Utility.OperateMode.Update:

                    btnSave.Enabled = true;
                    showInfo(_PayFeeID);
                    break;
                case Utility.OperateMode.Delete:
                    break;
                default:
                    break;
            }



        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtPayFeeUserID, (new P_User()).GetList(""), "UserID", "UserName");
        }


        private void showInfo(string sID)
        {
            mOrderPayFee.GetModel(sID);
            this.txtPayFeeID.Text = mOrderPayFee.PayFeeID;
            _OrderID = mOrderPayFee.OrderID;
            if (mOrderPayFee.PayFeeDate != null)
                this.txtPayFeeDate.DateTime = mOrderPayFee.PayFeeDate.Value;
            this.txtPayFeeUserID.EditValue = mOrderPayFee.PayFeeUserID;
            this.txtPayFee.Value = mOrderPayFee.PayFee.Value;
            this.txtRemark.Text = mOrderPayFee.Remark;

        }
    

        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            
            
            mOrderPayFee.PayFeeID = txtPayFeeID.Text;
            mOrderPayFee.OrderID = _OrderID;
            mOrderPayFee.PayFeeUserID = (string)txtPayFeeUserID.EditValue;
            mOrderPayFee.PayFee = txtPayFee.Value;
            mOrderPayFee.PayFeeDate = txtPayFeeDate.DateTime;
            mOrderPayFee.Remark = txtRemark.Text;

            if (mOrderPayFee.PayFee == 0)
            {
                XtraMessageBox.Show("请输入付款金额!");
                txtPayFee.Focus();
                return;
            }

            switch (OpMode)
            {
                case TierService.Utility.OperateMode.Insert:
                    try
                    {
                        mOrderPayFee.PayFeeID = Utility.GetSeqval(Utility.SeqName.PayFeeID.ToString());
                        mOrderPayFee.Add();
                        _PayFeeID = mOrderPayFee.PayFeeID;
                        txtPayFeeID.Text = mOrderPayFee.PayFeeID;
                        OpMode = Utility.OperateMode.Update;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("新增付款信息出错,错误原因为:" + ex.Message);
                        break;
                    }
                    MessageBox.Show("付款信息增加成功!");
                    DialogResult = DialogResult.OK;
                    break;
                case TierService.Utility.OperateMode.Update:
                    try
                    {
                        mOrderPayFee.Update();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("修改付款信息出错,错误原因为:" + ex.Message);
                        break;
                    }
                    MessageBox.Show("付款信息修改成功!");
                    DialogResult = DialogResult.OK;
                    break;
                default:
                    break;
            }
        }



    }
}